package com.company;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import java.util.stream.IntStream;

public class Main {

    public static void main(String[] args) {
	// write your code here
//        System.out.println(deleteTriple("aabcccbbad"));
        System.out.println(replaceTriple("aabcccbbad"));
    }

    static String deleteTriple(String str)
    {
        Stack<Character> stack = new Stack<Character>();
        int n = str.length();
        out : while(true) {
            int duplicates = 0;
            int i = 0;
            in: while(i <= str.length()-1) {
                char c = str.charAt(i);
                if (!stack.isEmpty() &&  c == stack.peek()) {
                    //如果跟栈顶元素相同 ,直接入栈，但是记录出现重复字母次数记录
                    stack.push(str.charAt(i));
                    duplicates += 1;
                } else {
                    if (duplicates == 0) {
                        stack.push(str.charAt(i));
                    } else if (duplicates >= 2) {
                        //出现三次重复相同入栈
                        StringBuffer buffer = new StringBuffer();
                        for (int y = 0; y <= duplicates; y++) {
                            Character pop = stack.pop();
                            buffer.append(pop);
                        }
                        //删除重复字符
                        str = str.replace(buffer.toString(), "");
                        System.out.println(str);
                        stack = new Stack<Character>();
                        continue out;
                    }else if(duplicates == 1){
                        duplicates = 0;
                        stack.push(str.charAt(i));
                    }
                }
                i++;
            }
            break;
        }
        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty()) {
            sb.append(stack.pop());
        }
        return sb.reverse().toString();
    }

    static String replaceTriple(String str)
    {
        ArrayList<Character> list = new ArrayList<>();
        Map<Character,Integer> map = new HashMap<>();
        int nn = 0;
        for (int i = 'a'; i <='z' ; i++) {
            list.add((char) i);
            map.put((char) i,nn);
            nn++;
        }
        Stack<Character> stack = new Stack<Character>();
        int n = str.length();
        out : while(true) {
            int duplicates = 0;
            int i = 0;
            in: while(i <= str.length()-1) {
                char c = str.charAt(i);
                if (!stack.isEmpty() &&  c == stack.peek()) {
                    //如果跟栈顶元素相同 ,直接入栈，但是记录出现重复字母次数记录
                    stack.push(str.charAt(i));
                    duplicates += 1;
                } else {
                    if (duplicates == 0) {
                        stack.push(str.charAt(i));
                    } else if (duplicates >= 2) {
                        //出现三次重复相同入栈
                        StringBuffer buffer = new StringBuffer();
                        Character pop = null;
                        for (int y = 0; y <= duplicates; y++) {
                            pop = stack.pop();
                            buffer.append(pop);
                        }
                        //替换字符
                        Integer integer = map.get(pop);
                        if(integer != 0){
                            Character character = list.get(--integer);
                            str = str.replace(buffer.toString(), character+"");
                            System.out.println(str +", "+ buffer.toString()+ " is replaced by "+character);
                        }else{
                            str = str.replace(buffer.toString(), "");
                            System.out.println(str +", "+ buffer.toString()+ " is replaced by ");
                        }

                        stack = new Stack<Character>();
                        continue out;
                    }else if(duplicates == 1){
                        duplicates = 0;
                        stack.push(str.charAt(i));
                    }
                }
                i++;
            }
            break;
        }
        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty()) {
            sb.append(stack.pop());
        }
        return sb.reverse().toString();
    }

}
